package com.carple.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.carple.dao.BoardDao;
import com.carple.dto.Board;

@WebServlet("/board/view.action")
public class BoardViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		if (request.getSession().getAttribute("loginuser") == null) {
			response.sendRedirect(
				"/carple/account/loginform.action?" + 
				"returnuri=" + request.getRequestURI());
			return;
		}
		
		
		
		String boardNo = request.getParameter("boardno");
		if(boardNo==null || boardNo.length() == 0){
			response.sendRedirect("/carple/board/list.action");
			return;
		}

		BoardDao dao = new BoardDao();
		Board board = dao.viewContent(request.getParameter("boardno"));
		
		if(board == null){
			response.sendRedirect("/carple/board/list.action");
			return;
		}
		System.out.println(board.getBoardNo());
		dao.increaseBoardReadCount(board.getBoardNo());
		board.setReadCount(board.getReadCount() + 1);//+1 : 조회되기 전의 카운트가 나오므로 +1 해줌
		
		//조회된 데이터를 jsp에서 처리할수 있도록 request객체에 저장
		request.setAttribute("board", board);
				
		RequestDispatcher dispatcher = 
				request.getRequestDispatcher("/WEB-INF/views/board/boardview.jsp");

			dispatcher.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doGet(request, response);
	}

}
